package com.qiandw.myblog.controller.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiandw.myblog.entity.Post;
import com.qiandw.myblog.service.CategoryService;
import com.qiandw.myblog.service.CommentService;
import com.qiandw.myblog.service.PostService;
import com.qiandw.myblog.service.UserService;
import com.qiandw.myblog.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 页面控制器群
 *
 * @author KG
 */
@Slf4j
@Controller
public class IndexController {
    @Autowired
    private HttpServletRequest req;
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/index")
    public String index() {
        Page<Post> page = new Page<>();
        page.setCurrent(1);
        page.setSize(10);

        IPage<Map<String, Object>> pageData = postService.pageMaps(page, null);

        //添加关联的用户信息
        userService.join(pageData, "user_id");

        req.setAttribute("pageData", pageData);

        log.info("--------------->" + pageData.getRecords());
        log.info("-------------------------------" + page.getPages());
        return "index";
    }
}
